package cn.iocoder.yudao.module.vote.service.voterecord;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.vote.controller.admin.voteuserresult.vo.VoteOnlineUpdateReqVO;
import cn.iocoder.yudao.module.vote.service.voterecorddetail.VoteRecordDetailService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.vote.controller.admin.voterecord.vo.*;
import cn.iocoder.yudao.module.vote.dal.dataobject.voterecord.VoteRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.vote.convert.voterecord.VoteRecordConvert;
import cn.iocoder.yudao.module.vote.dal.mysql.voterecord.VoteRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.vote.enums.ErrorCodeConstants.*;

/**
 * 投票记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class VoteRecordServiceImpl implements VoteRecordService {

    @Resource
    private VoteRecordMapper recordMapper;
    @Resource
    private VoteRecordDetailService detailService;

    @Override
    public Integer createRecord(VoteRecordCreateReqVO createReqVO) {
        // 插入
        VoteRecordDO record = VoteRecordConvert.INSTANCE.convert(createReqVO);
        recordMapper.insert(record);
        // 返回
        return record.getId();
    }

    @Override
    public void updateRecord(VoteRecordUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRecordExists(updateReqVO.getId());
        // 更新
        VoteRecordDO updateObj = VoteRecordConvert.INSTANCE.convert(updateReqVO);
        recordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecord(Integer id) {
        // 校验存在
        this.validateRecordExists(id);
        // 删除
        recordMapper.deleteById(id);
    }


    /**
     * 通过投票id删除投票记录
     * @param voteId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByVoteId(Integer voteId){
        detailService.deleteByVoteId(voteId);
        return recordMapper.delByVoteId(voteId);
    }

    private void validateRecordExists(Integer id) {
        if (recordMapper.selectById(id) == null) {
            throw exception(RECORD_NOT_EXISTS);
        }
    }

    @Override
    public VoteRecordDO getRecord(Integer id) {
        return recordMapper.selectById(id);
    }

    @Override
    public List<VoteRecordDO> getRecordList(Collection<Integer> ids) {
        return recordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<VoteRecordDO> getRecordPage(VoteRecordPageReqVO pageReqVO) {
        return recordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VoteRecordDO> getRecordList(VoteRecordExportReqVO exportReqVO) {
        return recordMapper.selectList(exportReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer createRecord(VoteOnlineUpdateReqVO onlineReq) {
        VoteRecordDO voteRecordDO = new VoteRecordDO();
        voteRecordDO.setVoteId(onlineReq.getVoteId());
        voteRecordDO.setIp(onlineReq.getIp());
        voteRecordDO.setVoteName(onlineReq.getVoteName());
        voteRecordDO.setVoteCount(onlineReq.getVoteStatusList().size());
        voteRecordDO.setVoteType(onlineReq.getVoteType());
        voteRecordDO.setVotePersonType(onlineReq.getVotePersonType());
        voteRecordDO.setVoteMode(onlineReq.getVoteMode());
        voteRecordDO.setTenantId(onlineReq.getTenantId());
        recordMapper.insert(voteRecordDO);
        detailService.insertBatchVote(onlineReq,voteRecordDO.getId());
        return voteRecordDO.getId();
    }


    @Override
    public Boolean isVote(Integer voteId){
        Long id = SecurityFrameworkUtils.getLoginUserId();
        return recordMapper.countByCreater(id,voteId)>0;
    }

    @Override
    public Boolean isVote(Integer voteId,String ip){
        return recordMapper.countByIp(ip,voteId)>0;
    }


    @Override
    public Boolean ipIsVote(Integer voteId,String ip){
        return recordMapper.countByIp(ip,voteId)>0;
    }

}
